/*
 *  Shopbutton.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 10/30/12.
 *  Copyright 2012 The Hex Pistols. All rights reserved.
 *
 *  This class is only used on MarketScene. It has a large sprite sheet and changes color based 
 *  on a "price difference" between two goods. It SHOULD inherit GuiButton, but trying to do this
 *  gave us a lot of problems so we decided to go the way of poor design and easy functionality in
 *  in the short run.
 *  
 */

#include "Shopbutton.h"
#include <string>
#include <sstream>
	

ShopButton::ShopButton() : bounds(SDL_Rect()), icon(NULL), itemNameLabel(NULL), qtyLabel(NULL), state(0), isHover(false), lastClicked(0)
{
	isHover = false;
	lastClicked = false;
	int qty = 0;

	TTF_Font *Font = TTF_OpenFont( "Futura.ttc", 20 );

	SDL_Color labelColor = {255,255,255};
	itemNameLabel = TTF_RenderText_Blended(Font, "", labelColor);
	qtyLabel = TTF_RenderText_Blended(Font, "", labelColor);

	std::string itemName = "";

    
	icon = load_image("ShopButton.png",false);
	if (icon == NULL) {
		std::cerr << "Error: Loading ShopButton Image" << std::endl;
	}
    
    bounds.x = 0;
    bounds.y = 0;
    bounds.w = icon->w;
    bounds.h = icon->h/8;
    state = 0;
    
    //sprite clips, evens are none, odds are mouseovers
    clip[0].x = 0;
    clip[0].y = 0;
    clip[0].w = 193;
    clip[0].h = 42;
    
    clip[1].x = 0;
    clip[1].y = 45;
    clip[1].w = 193;
    clip[1].h = 42;
    
    clip[2].x = 0;
    clip[2].y = 90;
    clip[2].w = 193;
    clip[2].h = 42;
    
    clip[3].x = 0;
    clip[3].y = 135;
    clip[3].w = 193;
    clip[3].h = 42;
    
    clip[4].x = 0;
    clip[4].y = 180;
    clip[4].w = 193;
    clip[4].h = 42;
    
    clip[5].x = 0;
    clip[5].y = 225;
    clip[5].w = 193;
    clip[5].h = 42;
    
    clip[6].x = 0;
    clip[6].y = 270;
    clip[6].w = 193;
    clip[6].h = 42;
    
    clip[7].x = 0;
    clip[7].y = 315;
    clip[7].w = 193;
    clip[7].h = 42;
	
	//Create SDL_Surface for label
	if (itemName != ""){
		SDL_Color labelColor = {255,255,255};
		itemNameLabel = TTF_RenderText_Blended(getFont(itemName), itemName.c_str(), labelColor);
        std::ostringstream out;
        out << qty;
        qtyLabel = TTF_RenderText_Blended(getFont(out.str()), out.str().c_str(), labelColor);
    }
	TTF_CloseFont(Font);
}

ShopButton::ShopButton(const ShopButton &S) : bounds(S.bounds), icon(S.icon), itemNameLabel(S.itemNameLabel), qtyLabel(S.qtyLabel),
                                                state(S.state), isHover(S.isHover), lastClicked(S.lastClicked)
{
    for (int i = 0; i < 8; i++){
        clip[i] = S.clip[i];
    }
}

ShopButton::~ShopButton()
{
	SDL_FreeSurface(icon);
    if (itemNameLabel != NULL){
        SDL_FreeSurface(itemNameLabel);
        SDL_FreeSurface(qtyLabel);
    }
}

shopbutton_state_t ShopButton::handle_input(SDL_Event event)
{
	//If a key was pressed
	if (event.type == SDL_MOUSEBUTTONDOWN) {
		SDL_Rect mouse;
		mouse.x = event.button.x;
		mouse.y = event.button.y;
		mouse.w = 1;
		mouse.h = 1;
		if (check_collision(bounds, mouse)) {
			//std::cout << "Clicked" << std::endl;
			return SHOP_CLICKED;
		}

	} else if (event.type == SDL_MOUSEMOTION) {
        //the mouse moved
        
		SDL_Rect mouse;
		mouse.x = event.motion.x;
		mouse.y = event.motion.y;
		mouse.w = 1;
		mouse.h = 1;
        
        //react if mouse is over the button
		if (check_collision(bounds, mouse)) {
            if (state % 2 == 0 && !isHover){
				if (!lastClicked) state++;
                isHover = true;
            }
			return SHOP_MOUSEOVER;
		} else {
			if (state % 2 == 1 && isHover){
                if (!lastClicked) state--;
                isHover = false;
            }
			return SHOP_NONE;
		}
	}
	return SHOP_NONE;
}

void ShopButton::setInfo(std::string itemName, int qty)
{
	SDL_FreeSurface(itemNameLabel);
	SDL_FreeSurface(qtyLabel);
    SDL_Color labelColor = {255,255,255};
	TTF_Font *font1 = TTF_OpenFont("Futura.ttc", 20);
	
    itemNameLabel = TTF_RenderText_Blended(font1, itemName.c_str(), labelColor);
    std::ostringstream out;
    out << qty;
	
	TTF_Font *font2 = TTF_OpenFont("Futura.ttc", 20);
    qtyLabel = TTF_RenderText_Blended(font2, out.str().c_str(), labelColor);
	
	TTF_CloseFont(font1);
	TTF_CloseFont(font2);

}

/*
 *  Sets the color based on priceDifference, intended to be the difference between the Shop's price and 
 *  the Player's price of a single good. Negative->red, positive->green, even->yellow.
 */
void ShopButton::setColor(int priceDifference){
    if (priceDifference > 0){
        state = 2;
    } else if (priceDifference == 0){
        state = 4;
    } else {
        state = 6;
    }
}

void ShopButton::show(SDL_Surface *screen)
{
	apply_surface_clip(bounds.x, bounds.y, clip[state], icon, screen);
    if (itemNameLabel != NULL){
        apply_surface(bounds.x+5, bounds.y+bounds.h/2-itemNameLabel->h/2, itemNameLabel, screen);
        apply_surface(bounds.x+bounds.w-qtyLabel->w-25, bounds.y+bounds.h/2-qtyLabel->h/2,
                      qtyLabel, screen);
    }
	
}

/*
 *  Sets the 
 */
void ShopButton::setCoords(int x, int y) {
	this->bounds.x = x;
	this->bounds.y = y;
}

void ShopButton::setState(int state){
    if (-1 < state && state < 8){
        this->state = state;
    }
}

int ShopButton::getState(){
    return state;
}

/*
 *  Calculates standard font with appropriate size for text to be placed on a button
 */
TTF_Font* ShopButton::getFont(std::string s)
{
    return TTF_OpenFont("Futura.ttc", 20);
}

void ShopButton::setLastClicked(bool b) {
	//this button is becoming last clicked
	/*if (b && !lastClicked) {
		state++;
	}
	//this button is becoming NOT last clicked
	else*/ if (!b && lastClicked) {
		state--;
	}
	lastClicked = b;
	
}
